﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Delay;
using System.ComponentModel;

namespace PhoneInfo.Page
{
    public partial class SearchResult : PhoneApplicationPage
    {
        SearchResultView searchResultNewView = null;
        SearchResultView searchResultHotView = null;
        private string comparePid = "";
        private string compareName = "";
        private string compareUrl = "";
        private string comppid = "";
        private string comphonename = "";
        private IDictionary<string, string> parameters;
        public SearchResult()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            parameters = this.NavigationContext.QueryString;
            if (parameters.ContainsKey("pid_1"))
                comparePid = parameters["pid_1"];
            if (parameters.ContainsKey("ppname_1"))
                compareName = parameters["ppname_1"];
            if (parameters.ContainsKey("picurl_1"))
                compareUrl = parameters["picurl_1"];
            if (parameters.ContainsKey("ppid_1"))
                comppid = parameters["ppid_1"];
            if (parameters.ContainsKey("phonename_1"))
                comphonename = parameters["phonename_1"];
            if (searchResultNewView == null)
            {
                searchResultNewView = new SearchResultView();
                searchResultNewView.ClassName = "searchResultNewView";
                ProgBar.Visibility = Visibility.Visible;
                searchResultNewView.Result = 0;
                searchResultNewView.CurrentPage = 1;
                searchResultNewView.TotalPage = 0;
                searchResultNewView.PageSize = 20;
                searchResultNewView.AllowAdd = false;
                searchResultNewView.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(searchResultNewView.GetPhoneInfoUrl(parameters, 0, searchResultNewView.CurrentPage)));
                searchResultNewView.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                New.DataContext = searchResultNewView;
            }
        }
        void type_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == null) return;
            if (sender is SearchResultView)
            {
                SearchResultView se = (SearchResultView)sender;
                if (se == null) return;
                if (e.PropertyName == "Result")
                {
                    ProgBar.Visibility = Visibility.Collapsed;
                    switch (se.Result)
                    {
                        case -1:
                        case 1:
                            ToastPrompt toastPrompt = new ToastPrompt()
                                              {
                                                  Message = se.Message
                                              };
                            toastPrompt.Show();
                            break;
                        case 2:
                            break;
                    }
                }
            }
        }
        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender == null) return;
            Pivot pivot = sender as Pivot;
            if (pivot == null) return;
            switch (pivot.SelectedIndex)
            {
                case 0:
                    //最新
                    LayoutRoot.DataContext = searchResultNewView;
                    break;
                case 1:
                    //最热
                    if (searchResultHotView == null)
                    {
                        searchResultHotView = new SearchResultView();
                        searchResultHotView.ClassName = "searchResultHotView";
                        searchResultHotView.Result = 0;
                        searchResultHotView.CurrentPage = 1;
                        searchResultHotView.TotalPage = 0;
                        searchResultHotView.PageSize = 20;
                        searchResultHotView.AllowAdd = false;
                        searchResultHotView.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(type_PropertyChanged);
                        searchResultHotView.SetValue(LowProfileXmlLoader.UriSourceProperty,
                                                     new Uri(searchResultNewView.GetPhoneInfoUrl(parameters, 1, searchResultHotView.CurrentPage)));
                        Hot.DataContext = searchResultHotView;
                    }
                    break;
            }
        }

        private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
        {
            SearchResultView infoView = (SearchResultView)((ListBox)sender).DataContext;
            ScrollViewer viewer = FindSimpleVisualChild<ScrollViewer>((ListBox)sender);
            if (viewer.VerticalOffset >= viewer.ScrollableHeight / 4)
            {
                if (((ListBox)sender).DataContext is SearchResultView)
                {
                    if (infoView.CurrentPage * infoView.PageSize < infoView.TotalPage)
                    {
                        if (infoView.CurrentPage * infoView.PageSize < infoView.TotalPage)
                        {
                            infoView.AllowAdd = true;
                            infoView.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(infoView.GetPhoneInfoUrl(parameters, 0, searchResultNewView.CurrentPage + 1)));
                        }
                    }
                }

            }
        }
        VisualStateGroup FindVisualState(FrameworkElement element, string name)
        {
            if (element == null) return null;
            System.Collections.IList groups = VisualStateManager.GetVisualStateGroups(element);
            foreach (VisualStateGroup group in groups)
                if (group.Name == name) return group;
            return null;
        }
        public static T FindSimpleVisualChild<T>(DependencyObject element) where T : class
        {
            //Debug.WriteLine("FindSimpleVisualChild");
            while (element != null)
            {
                if (element is T)
                    return element as T;
                else
                    element = VisualTreeHelper.GetChild(element, 0);
            }
            return null;
        }
        private void ListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null) return;
            ListBox lb = sender as ListBox;
            if (lb == null) return;
            phonePkItem pkItem = lb.SelectedItem as phonePkItem;
            if (pkItem == null) return;
            if (comparePid.Length == 0)
            {
                string objective = "/Page/TypeData.xaml";
                objective += "?pid=" + pkItem.Ponhe1_pid;
                objective += "&price=" + pkItem.Ponhe1_price;
                objective += "&maxprice=" + pkItem.Ponhe1_maxprice;
                objective += "&minprice=" + pkItem.Ponhe1_minprice;
                objective += "&picurl=" + pkItem.Ponhe1_picurl;
                objective += "&phonename=" + pkItem.Ponhe1_phonename;
                objective += "&ppname=" + pkItem.Ponhe1_ppname;
                NavigationService.Navigate(new Uri(objective, UriKind.Relative));
            }
            else
            {
                string objective = "/Page/PhonePk.xaml";
                objective += "?pid1=" + comparePid;
                objective += "&name1=" + compareName + "\r\n" + comphonename;
                objective += "&phonename1=" + comphonename;
                objective += "&ppname1=" + compareName;
                objective += "&ppid=" + comppid;
                objective += "&picurl1=" + compareUrl;
                objective += "&pid2=" + pkItem.Ponhe1_pid;
                objective += "&ppid2=" + pkItem.Ponhe1_ppid;
                objective += "&picurl2=" + pkItem.Ponhe1_picurl;
                objective += "&name2=" + pkItem.Ponhe1_phonename + "\r\n" + pkItem.Ponhe1_ppname;
                objective += "&phonename2=" + pkItem.Ponhe1_phonename;
                objective += "&ppname2=" + pkItem.Ponhe1_ppname;
                NavigationService.Navigate(new Uri(objective, UriKind.Relative));
            }
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { }
    }
}